<template>
  <div class="news">
    <div>
      <h2 style="display: inline-block; margin-right: 10px;">{{ news.title }}</h2>
      <el-tag v-if="newsEdit(this.roleNo)" style="float: right;cursor:pointer;">
        <router-link :to="{path: 'newsEdit', query: {id:this.newsId}}">编辑</router-link>
      </el-tag>
    </div>

    <div class="news-info">
      <div class="info-item">
        <strong>发布时间：</strong>{{ news.createdTime }}
        <strong> | 责任编辑：</strong>{{ news.editor }}
      </div>
    </div>
    <div class="news-content" v-html="processedContent"></div>
  </div>
</template>

<script>
import request from "@/util/request.js";
import {newsEdit} from "@/util/permissionJudgment.js";

export default {
  data() {
    return {
      roleNo: [],
      news: {},
      newsId: this.$route.query.id,
      processedContent: ''
    }
  },
  created() {
    this.getContent(this.newsId)
    this.setRoleNo()
  },
  methods: {
    newsEdit,
    setRoleNo() {
      if (JSON.parse(localStorage.getItem("user")).roleNo.includes(",")) {
        this.roleNo = JSON.parse(localStorage.getItem("user"))["roleNo"].split(',')
      } else {
        this.roleNo.push(JSON.parse(localStorage.getItem("user"))["roleNo"])
      }
    },
    getContent(id) {
      request.get("/related_news/findOneById?id=" + id).then(res => {
        this.news = res.data.data;
        this.processedContent = this.replaceImageAndVideoStyles(this.news.content);
      })
    },
    replaceImageAndVideoStyles(content) {
      // 替换图片和视频标签的样式
      return content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:0 auto;" ')
          .replace(/<video/gi, '<video style="max-width:100%;height:auto;display:block;margin:0 auto;" ');
    }
  }
}

</script>

<style scoped>
.news {
  text-align: center;
  margin: 0 auto;
  max-width: 80%;
  padding: 20px;
  border-radius: 10px;
  background: linear-gradient(45deg, #87CEFA 0%, #C0F0F1 20%, #A0E2EF 39%, #68B5EB 76%, #7CACD2 100%);
  box-shadow: 0 0 4px 2px rgba(0, 0, 0, .4);
}

.news h2 {
  margin-bottom: 20px;
  color: #333;
  font-size: 28px;
}

.info-item strong {
  font-weight: bold;
}

.news-content {
  text-align: left;
  margin-bottom: 20px;
}

/* 为段落内的图片添加样式 */
.news-content p img {
  text-indent: 2em;
}

/* 为图片和视频添加样式，使其居中显示 */
.news-content img,
.news-content video {
  max-width: 100%;
  height: auto; /* 让图片高度自适应，防止拉伸变形 */
  display: block;
  margin: 0 auto; /* 居中显示 */
  margin-top: 20px;
}

@media (max-width: 1500px) {
  .news {
    width: 100%;
    max-width: 100%;
  }
}
</style>
